import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Test;

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.json.JSONConfiguration;

public class GoogleCalendarClient {
	final String CLIENT_ID = "170966694283-0lmambqf7n06ugkmsu269jlkdniued2t.apps.googleusercontent.com";
	final String uri = "https://accounts.google.com/o/oauth2/device/code";

	@Test
	public void testClientConnection() throws IOException, JSONException {

		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost(uri);

		// post.setParams(new BasicHttpParams().setParameter("client_id",
		// CLIENT_ID));
		// post.setParams(new BasicHttpParams().setParameter("scope",
		// "emnnnail"));

		HashMap<String, String> objectTokens = new HashMap<String, String>();
		objectTokens.put("client_id", CLIENT_ID);
		objectTokens.put("scope", "email");
		JSONObject object = new JSONObject(objectTokens);
//		object.put("client_id", CLIENT_ID);
//		object.put("scope", "email");
		


		StringEntity input = new StringEntity("{\"client_id\":\"170966694283-0lmambqf7n06ugkmsu269jlkdniued2t.apps.googleusercontent.com\",\"scope\":\"email\"}");
		 input.setContentType("application/json");
		post.setEntity(input);
		HttpResponse response = client.execute(post);
		BufferedReader rd = new BufferedReader(new InputStreamReader(response
				.getEntity().getContent()));
		String line = "";
		while ((line = rd.readLine()) != null) {
			System.out.println(line);

		}

	}

	@Test
	public void test2() throws JSONException {
		JSONObject object = new JSONObject();
		object.put("client_id", CLIENT_ID);
		object.put("scope", "email");

		ClientConfig conf = new DefaultClientConfig();
		conf.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING,
				Boolean.TRUE);
		Client client = Client.create(conf);
		WebResource webResource = client.resource(uri);
		ClientResponse response = webResource.accept("application/json")
				.type("application/json").post(ClientResponse.class, object);
		if (response.getStatus() != 200) {
			throw new RuntimeException("Failed : HTTP error code : "
					+ response.getStatus());
		}

		String output = response.getEntity(String.class);

		System.out.println("Server response .... \n");
		System.out.println(output);

	}

	@Test
	public void test3() throws JSONException {
		// Step2: Now pass JSON File Data to REST Service
		JSONObject object = new JSONObject();
		object.put("client_id", CLIENT_ID);
		object.put("scope", "email");
		try {
			URL url = new URL(uri);
			URLConnection connection = url.openConnection();
			connection.setDoOutput(true);
			connection.setRequestProperty("Content-Type", "application/json");
			connection.setConnectTimeout(5000);
			connection.setReadTimeout(5000);
			OutputStreamWriter out = new OutputStreamWriter(
					connection.getOutputStream());
			out.write(object.toString());
			out.close();

			BufferedReader in = new BufferedReader(new InputStreamReader(
					connection.getInputStream()));
			String line;
			while ((line =in.readLine()) != null) {
				System.out.println(line);
			}
			System.out.println("\nREST Service Invoked Successfully..");
			in.close();
		} catch (Exception e) {
			System.out.println("\nError while calling REST Service");
			System.out.println(e);
		}
	}
	
	@Test
	public void test4(){
		//Rest uris
		String calendarUri = "https://www.googleapis.com/calendar/v3";
		String getCal ="/calendars/calendarId/acl/ruleId";

		String calendarList ="https://www.googleapis.com/calendar/v3/users/me/calendarList?parameters";
		//Auth
		String scope = "https://www.googleapis.com/auth/calendar";
		String device_code = "4/pIYjD6uqTeUhzIGQXrSslSByCuBo";
		
		String accessToken = "ya29.eABYkceCFYzz8hwAAADogZpnKASHR3pzUIp1TyaCSxYoAbq8TFr7Ve-T8GJmjw";
		
		GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
		
	}
}
